/**  
 * @Title: BeanToMapUtils.java
 * @Package com.hl.card.credit.pub.utils
 * @Description: TODO(用一句话描述该文件做什么)
 * @author wdf
 * @date 2018年3月31日
 * @version V1.0  
 */

package com.hl.card.credit.pub.utils;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import com.alibaba.fastjson.JSON;
import com.hl.card.credit.pub.entity.PublicOrders;

/**
 * @ClassName: BeanToMapUtils
 * @Description: TODO(BeanToMap)
 * @author wdf
 * @date 2018年3月27日
 *
 */

public class BeanToMapUtils {

	/**
	 * 将一个 Map 对象转化为一个 JavaBean
	 * (com.alibaba.fastjson.JSON)
	 * @param clazz
	 *            要转化的类型
	 * @param map
	 *            包含属性值的 map
	 * @return 转化出来的 JavaBean 对象
	 */
	public static <T> T toBean(Class<T> clazz, Map map) {
		return JSON.parseObject(JSON.toJSONString(map), clazz);
	}

	/**
	 * 将一个 JavaBean 对象转化为一个 Map
	 * 
	 * @param bean
	 *            要转化的JavaBean 对象
	 * @return 转化出来的 Map 对象
	 * @throws IntrospectionException
	 *             如果分析类属性失败
	 * @throws IllegalAccessException
	 *             如果实例化 JavaBean 失败
	 * @throws InvocationTargetException
	 *             如果调用属性的 setter 方法失败
	 */
	@SuppressWarnings("rawtypes")
	public static Map toMap(Object bean) {
		Class<? extends Object> clazz = bean.getClass();
		Map<Object, Object> returnMap = new HashMap<Object, Object>();
		BeanInfo beanInfo = null;
		try {
			beanInfo = Introspector.getBeanInfo(clazz);
			PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
			for (int i = 0; i < propertyDescriptors.length; i++) {
//				System.out.println("toMap i:" + i);
				PropertyDescriptor descriptor = propertyDescriptors[i];
				String propertyName = descriptor.getName();
				if (!propertyName.equals("class")) {
					Method readMethod = descriptor.getReadMethod();
					Object result = null;
					result = readMethod.invoke(bean, new Object[0]);
					if (null != propertyName) {
						propertyName = propertyName.toString();
					}
					if (null != result) {
						result = result.toString();
					}
					returnMap.put(propertyName, result);
				}
			}
		} catch (IntrospectionException e) {
			System.out.println("分析类属性失败");
		} catch (IllegalAccessException e) {
			System.out.println("实例化 JavaBean 失败");
		} catch (IllegalArgumentException e) {
			System.out.println("映射错误toMap");
		} catch (InvocationTargetException e) {
			System.out.println("调用属性的 setter 方法失败");
		}
		return returnMap;
	}

	public static void main(String[] args) {
		Map<Object, Object> map = new TreeMap<Object, Object>();
		map.put("busines_order_id", 2);
		map.put("remark", "dddddd");
		map.put("see", 123);
		// System.out.println(map);
		// PublicOrders p = new PublicOrders();
		// p.setBusines_order_id(2);
		// p.setRemark("dddddd");
		// @SuppressWarnings("unchecked")
		// Map<Object, Object> m = toMap(p);
		PublicOrders p2 = toBean(PublicOrders.class, map);
		// System.out.println(p);
		System.out.println(JSON.toJSONString(p2));
		System.out.println(map);

	}

}
